import type { Ref } from "vue";
import type { TableInstance } from "element-plus";
import { JxtechPageTableExpType } from "@whjxtech/jxtech-component";
import { JxtechPageTableDeleteConfig } from "../../types";
import { ServiceApi } from "@/api/modules/_base";

/**
 * 分页表格
 * @param pageTableExtRef JxtechPageTableExt ref对象
 */
export function useJxtechPageTableExt(pageTableExtRef: Ref) {
  /**加载表格 */
  const loadTable = () => pageTableExtRef.value.loadTable();

  /**导出数据 */
  const expData = (excType: JxtechPageTableExpType) => pageTableExtRef.value.expData(excType);

  /**
   * 重新加载表格
   */
  const reloadTable = () => pageTableExtRef.value.reloadTable();

  /**
   * 获取选中的数据
   */
  const getSelectionData = () => pageTableExtRef.value.getSelectionData();
  /**
   * 获取选中的数据的主键集合 需要配置 rowKey
   */
  const getSelectionPkList = () => pageTableExtRef.value.getSelectionPkList();

  /**
   * 清理选中
   */
  const clearSelection = () => pageTableExtRef.value.clearSelection();

  /**
   * @description 清理过滤
   * @param columnKeyArray 列key
   */
  const clearFilter = (columnKeyArray?: string[]) => pageTableExtRef.value.clearFilter(columnKeyArray);

  /**
   * 清理排序
   */
  const clearSort = () => pageTableExtRef.value.clearSort();

  /**
   * 获取el实例
   */
  const getElTableInstance = () => pageTableExtRef.value.getElTableInstance() as TableInstance;

  /**
   * 删除选中的数据
   * @param servicelApi service api
   * @param jxtechPageTableDeleteConfig 删除数据配置
   * @returns
   */
  const deleteSelectionDataList = (servicelApi: ServiceApi, jxtechPageTableDeleteConfig?: JxtechPageTableDeleteConfig) =>
    pageTableExtRef.value.deleteSelectionDataList(servicelApi, jxtechPageTableDeleteConfig);

  /**
   * 删除选中的主键数据
   * @param servicelApi service api
   * @param jxtechPageTableDeleteConfig 删除数据配置
   * @returns
   */
  const deleteSelectionPkList = (servicelApi: ServiceApi, deleteConfig?: JxtechPageTableDeleteConfig) =>
    pageTableExtRef.value.deleteSelectionPkList(servicelApi, deleteConfig);

  /**
   * 根据主键删除对象
   * @param servicelApi service api
   * @param jxtechPageTableDeleteConfig 删除数据配置
   * @returns
   */
  const deleteSelectionPk = (servicelApi: ServiceApi, deleteConfig?: JxtechPageTableDeleteConfig) =>
    pageTableExtRef.value.deleteSelectionPk(servicelApi, deleteConfig);

  return {
    loadTable,
    expData,
    reloadTable,
    getSelectionData,
    getSelectionPkList,
    clearSelection,
    clearFilter,
    clearSort,
    getElTableInstance,
    deleteSelectionDataList,
    deleteSelectionPkList,
    deleteSelectionPk
  };
}
